package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OperatorDao {
    private static final String TAG = "OperatorDao";
    private Dao<Operator, Integer> PipeInfoDao;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     *
     * @param context
     */
    public OperatorDao(Context context) {
        try {
            PipeInfoDao = DetailDataOpenHelper.getInstance(context).getDao(Operator.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     *
     * @param PipeInfo
     */
    public void add(Operator PipeInfo) {
        try {
            PipeInfoDao.create(PipeInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void update(Operator PipeInfo) {
        try {
            PipeInfoDao.update(PipeInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addAll(List<Operator> list, String cid) {
        try {
            deleteByCid(cid);
            if (list == null) {
                return;
            }
            for (Operator operator : list) {
                operator.setmId(cid);
//                if(TextUtils.equals(operator.getName(), Constant.ACTION_NAME_KSJB)){
                Log.e(TAG, "operatorName:" + JsonHelper.toJson(operator));
//                }
                PipeInfoDao.create(operator);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addAll(List<Operator> list, String cid, String name) {
        try {
            if (list == null) {
                return;
            }
            deleteByCid(cid);
            for (Operator operator : list) {
                operator.setmId(cid);
                if (TextUtils.equals(operator.getName(), Constant.ACTION_NAME_KSJB)) {
                    Log.e(TAG, "operatorName:" + JsonHelper.toJson(operator));
                }
                PipeInfoDao.create(operator);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     *
     * @param PipeInfo
     */
    public void delete(Operator PipeInfo) {
        try {
            PipeInfoDao.delete(PipeInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteByCid(String cid) {
        try {
            List<Operator> temps = queryForAllByMID(cid);
            if (temps != null) {
                for (Operator operator : temps) {
                    delete(operator);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询一条记录
     *
     * @param id
     * @return
     */
    public Operator queryForId(int id) {
        Operator PipeInfo = null;
        try {
            PipeInfo = PipeInfoDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfo;
    }


    /**
     * 查询所有记录
     *
     * @return
     */
    public List<Operator> queryForAll() {
        List<Operator> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    private List<Operator> quchong(List<Operator> data) {
        List<Operator> tempData = new ArrayList<>();
        if (data == null || data.isEmpty()) {
            return tempData;
        }
        for (Operator operator : data) {
            Operator tempO = getOperator(queryForAllByNum(operator.getmId(), operator.getNum()));
            if (tempO != null) {
                tempData.add(tempO);
                Log.e(TAG, "有重复");
            } else {
                tempData.add(operator);
            }
        }
        return tempData;
    }


    private Operator getOperator(List<Operator> data) {
        if (data == null || data.isEmpty() || data.size() == 1) {
            return null;
        }
        Operator operator = data.get(0);
        int index = operator.getId();
        for (Operator operator1 : data) {
            if (operator1.getId() > index) {
                index = operator1.getId();
                operator = operator1;
            }
        }
        deleteListWithout(data, operator.getId());
        return operator;
    }

    private void deleteListWithout(List<Operator> data, int id) {
        for (Operator operator : data) {
            if (operator.getId() != id) {
                delete(operator);
            }
        }
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<Operator> queryForAllByMID(String action) {
        List<Operator> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().orderBy("num", true).where().eq("m_id", action).query();
            test(PipeInfos);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return quchong(PipeInfos);
    }

    private void test(List<Operator> PipeInfos) {
        if (PipeInfos != null) {
            for (Operator o : PipeInfos) {
                Log.e(TAG, JsonHelper.toJson(o));
            }
        }
    }

    public List<Operator> queryForAllByNum(String cid, int action) {
        List<Operator> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where()
                    .eq("m_id", cid)
                    .and()
                    .eq("num", action)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    public void deleteByMid(String mid) {
        try {
            DeleteBuilder deleteBuilder = PipeInfoDao.deleteBuilder();
            deleteBuilder.where().eq("m_id", mid);
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
